home *** CD-ROM | disk | FTP | other *** search
/ Beginning Mac Programming / Beginning Mac Programming.bin / Open Me for REALbasic 3 / REALbasic 3.2 / Example Projects / Techniques / String Functions / string functions description < prev    next >
Text File  |  1998-02-24  |  10KB  |  253 lines

  1. Boolean Methods
  2.  
  3. The following methods return a boolean.  All take a single character as their
  4. parameter.  If a string is passed, they will ignore all but the first character
  5. of the string.  If a null string is passed, they will return false.
  6.  
  7.  
  8. Function isSpace(query as string) As Boolean 
  9. Returns true for space, return, and tab characters. 
  10. No dependency on other methods.
  11.  
  12.  
  13. Function isChar(query as string) As Boolean 
  14. Returns true for all letters and digits. 
  15. No dependency on other methods.
  16.  
  17.  
  18. Function isPunct(query as string) As boolean 
  19. Returns true for .,"';:! or ? 
  20. No dependency on other methods.
  21.  
  22.  
  23. Function isLetter(query as string) As boolean 
  24. Returns true for all letters a-z and A-Z .
  25. No dependency on other methods.
  26.  
  27.  
  28. The following method returns a boolean, but accepts a string as its parameter.
  29.  
  30. Function hasNonSpace(entry as string) As Boolean 
  31. Returns true if any character in the passed string is not a return, tab, or space. 
  32. Depends on method "isSpace"
  33.  
  34. Example use: 
  35. you can use any of these as a test in a for/next statement:
  36. if isPunct (theCharacter) then
  37.  
  38.  
  39. Integer Methods
  40.  
  41. Function outStr(start as integer, base as string, search as string) As integer
  42. Tells you how far from the right end of a string the last instance of the search string is.  If a negative number is passed as start, the return value will be the distance from the end of the string.  If zero or a positive value is passed, the distance from the start of the string will be returned
  43. No dependency on other methods
  44.  
  45. String Reduction Methods
  46.  
  47. These methods accept at least one string as their parameter and all return a
  48. string.  If a null string is passed for any parameter, they return a null string.
  49. For those which require an integer as a parameter, passing zero or a negative
  50. number will result in a null string being returned.
  51.  
  52.  
  53. Function nibbleLeft(entry as string) As String
  54. Function nibbleRight(entry as string) As String 
  55. Function nibble(entry as string) As String 
  56. Remove space characters from the leading end, trailing end, or both ends, respectively.
  57. Depend on method "isSpace"  nibble depends on each of the ended methods.
  58.  
  59.  
  60. Function chewRight(entry as string) As String 
  61. Function chewLeft(entry as string) As String 
  62. Function chew(entry as string) As String 
  63. Remove space and punctuation characters from the leading end, trailing end, or both
  64. ends, respectively.
  65. Depend on methods "isSpace" and "isPunct"  chew depends on each of the ended
  66. methods.
  67.  
  68.  
  69. Function chompRight(entry as string) As String
  70. Function chompLeft(entry as string) As String 
  71. Function chomp(entry as string) As String 
  72. Removes all characters which are not a letter or digit from the leading end, 
  73. trailing end, or both ends, respectively.
  74. Depends on method "isChar"  chomp depends on each of the ended methods.
  75.  
  76.  
  77. Examples:
  78. Let's say you had the following string (delineated by _'s):
  79. _   "&$(!*#, another bug in my code!", he said.
  80. _
  81. passing it to nibble would return:
  82. _"&$(!*#, another bug in my code!", he said._
  83. as the leading spaces and trailing return are trimmed.
  84. passing it to chew would give you:
  85. _&$(!*#, another bug in my code!", he said_
  86. as the spaces and punctuation are removed.
  87. chomping it would return:
  88. _another bug in my code!", he said_
  89. getting rid of all those nasty symbols at the front.
  90.  
  91. Function splitLeft(entry as string, delimiter as string) As string
  92. Function splitRight(entry as string, delimiter as string) As string
  93. find the first instance of the delimiter and return the appropriate side of the string.  Somewhat redundant, but faster than, the chompTo Functions.
  94. No dependency on other methods.
  95.  
  96.  
  97. Function nibbleToFromLeft(entry as string,target as string) As string 
  98. Removes any characters from the left end of the entry string up to and including 
  99. the first occurrance of the target string.
  100. No dependency on other methods.
  101.  
  102. Function nibbleToFromRight(entry as string,target as string) As string 
  103. Removes the last occurrance of the target string and any characters following
  104. it from the entry string.
  105. No dependency on other methods.
  106.  
  107. Function chompToFromLeft(entry as string, target as string) As String
  108. Removes all characters from the left end of the entry string up to and including
  109. the final occurrance of the target string.
  110. No dependency on other methods.
  111.  
  112. Function chompToFromRight(entry as string, target as string) As String 
  113. Removes the first occurance of the target string and all characters to the 
  114. right of it from the entry string.
  115. No dependency on other methods.
  116.  
  117. Function chewToFromLeft(entry as string, target as string, occurrance as integer) 
  118. As string
  119. Searches through the entry string for the target string.  If it fails to find a number 
  120. of target strings equal or greater to the passed integer, it returns the entry string.
  121. If it finds enough instances of the target string, it will remove all text up to and
  122. including the occuranceth instance.
  123. No dependencies on other methods. 
  124.  
  125. Function chewToFromRight(entry as string, targetas string, occurrance as integer) 
  126. As string 
  127. Finds all instances of the target string in the entry string.  If there are less
  128. instances than the passed integer, it returns the entry string.  Otherwise, it will
  129. remove everything including and to the right of the occurranceth instance of the
  130. target string.
  131. No dependencies on other methods.
  132.  
  133. Examples:
  134.  
  135. Let's say you had the string
  136. myString = "Now is the ti<>me for all go<>od men to joi<>n the party."
  137.  
  138. nibbleFromLeft (myString, "<>") would return:
  139. me for all go<>od men to joi<>n the party.
  140. chewFromLeft would return:
  141. n the party.
  142. and chewToFromLeft (myString, "<>", 2) would return:
  143. od men to joi<>n the party.
  144.  
  145.  
  146. Case Sensitive Methods:
  147.  
  148. Function csInStr(start as integer, base as string, query as string) As integer
  149. Like inStr, but takes case into account.
  150.  
  151. Function csSplitLeft(base as string, target as string) As string
  152. Function csSplitRight(base as string, target as string) As string
  153. Split the base string at the target string, taking case into account
  154. Depend on csInStr
  155.  
  156. Function csReplaceAll(base as string, target as string, replacement as string) As string
  157. Taking case into account, replaces every instance of target in base with replacement.
  158. depends on csInStr, csSplitLeft, csSplitRight
  159.  
  160.  
  161. Get Text Methods:
  162. These methods parse a string and return the requested instance of a specific type of text.
  163.  
  164. Function getWord(entry as string, instance as integer) As string
  165. Words here are defined as one or more alpha-numeric characters separated from other characters by a space, tab, or return.  Will concatenate a word at the end of a line that ends in a hyphen-return combination with the word on the following line.
  166.  
  167. Function getBracketedText(leftBracket as string, rightBracket as string, 
  168. instance as integer, entry as string) As String
  169. Finds occurrance number instance of a block of text surrounded by the two passed
  170. bracket string.  Ignores blocks which are comprised entirely of space characters.
  171. Reduces nested brackets.  Returns a null string if any of the passed strings are 
  172. null or the passed integer is less than 1.  Returns a null string if there are 
  173. less than instance occurrances of bracketed text.  The left and right brackets are 
  174. allowed to be identical characters.
  175. Depends on method "hasNonSpace"
  176.  
  177. Examples:
  178. Let's say you're searching for text within an HTML document, which you've imported
  179. as a string:
  180. myString = "<HTML>
  181. <b><i>My Page</b></i>
  182.  
  183. <b> This page has no content</b>
  184. "
  185.  
  186. Now, you set up a loop that repeatedly calls the get bracketed text, feeding the results
  187. into an array:
  188.  
  189. do
  190.     loopCounter = loopCounter + 1
  191.     tempString = getBracketedText (">", "<", loopCounter, myString)
  192.     if tempString = "" then
  193.         exit
  194.     else
  195.         myArray.append tempString
  196.     end if
  197.     loopCounter = loopCounter + 1
  198. loop
  199.  
  200. This will fill the array with all instances of the text bracketed by > and <.  I'll
  201. go through the string and tell you what's in or out as far as the array's concerned.
  202.  
  203. The first instance of the left bracket is after HTML.  The next right bracket is
  204. before the b, but they surround a return, which is a space character, so this is
  205. ignored.  The next right bracket is before the i, but there is another occurrance
  206. of the left bracket next to it.  Since nested brackets are reduced to a single item,
  207. the left bracket near HTML is ignored, and the one near b is evaluated.  There's
  208. nothing in between, so this is ignored, too.
  209. The next right bracket is next to Page.  It's left bracket is before My, so "My Page"
  210. is the first string returned, and therefore becomes the first string added to the
  211. array.
  212. The two brackets between the tags following page are ignored because they surround
  213. a null string.  The two returns between /i> and <b are ignored, since returns are
  214. space characters.  " This page has no content" will be the next item added.  After 
  215. that, there are no more occurrances of bracketed text, so a null string is returned,
  216. and the loop exits.
  217.  
  218. The idea here is that, although many things are ignored, the method is designed to 
  219. give you a high probability of returning useful content.
  220.  
  221.  
  222.  
  223. Objects:
  224. These were created to give you access to all of a type of text that is parsed from an entry string you provide.  To use them, create an instance of the object, then supply the string to the appropriate method, and a boolean value will be returned to indicate whether any instances of the text were found.  Instances are stored in an array.  These depend on many of the simple methods described above.
  225.  
  226. bracketedObject
  227. Works like getBracketed text, but finds all instances of bracketed text.
  228. Call:  
  229. Function findAll(leftBracket as string, rightBracket as string, entry as string) As Boolean
  230. Instance variables:
  231. boolean anyFound, string array result, integer totalFound
  232.  
  233. wordObject
  234. Finds all words in the passed string
  235. Call:
  236. Function findAll (entry as string) as boolean
  237. instance variables:
  238. boolean anyFound, string array result, integer totalFound
  239.  
  240. splitObject
  241. Divides a string into left and right parts at a delimiter you specify
  242. Call:
  243. Function split(entry as string, delimiter as string) As Boolean
  244. Instance variables:
  245. left and right as string
  246.  
  247. parseObject:
  248. Divides the passed string into substrings at any instance of the delimiter.
  249. Call:
  250. Function parse(entry as string, delimiter as string) As Boolean
  251. Instance variables:
  252. boolean anyPresent, string array result, integer numberPresent
  253.